import Editor from "./../editor";
import { throttle } from "../utils";
/** 键盘事件 */
export class SelectionChange {
    private _editor :Editor;

    constructor (editor:Editor) {
        this._editor = editor;
    }

    _selectionChangeHandler() {
        const domSelection = window.getSelection()
        this._editor.setSelection(domSelection?.anchorOffset || 0,domSelection?.focusOffset || 0)
    }

    /** 事件监听 */
    addListeners() {
        document.addEventListener(
            'selectionchange', 
            throttle(this._selectionChangeHandler.bind(this), 400)
        );
    }
    //提供卸载
    removeListeners(){
        document.removeEventListener("selectionchange",this._selectionChangeHandler)
    }
}